package com.wei.czz.framework.common.controller;

import com.wei.czz.common.annotation.Resubmit;
import com.wei.czz.common.enums.index.validateCode.ValidateCodeTypeEnum;
import com.wei.czz.common.tool.Result;
import com.wei.czz.common.utils.RegexUtils;
import com.wei.czz.framework.common.service.MailService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2022-04-28 19:18:51
 * className: MailController 邮件服务请求接口类
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("index/mail")
@AllArgsConstructor
public class MailController {

    private static final Logger log = LoggerFactory.getLogger(MailController.class);

    private final MailService mailService;

    /**
     * 发送电子邮件接口
     * <p>
     * 调用方：
     *  1、浏览器-登录/注册页面
     *  2、浏览器-后台管理页-用户个人信息管理页面调用
     * <p>
     * 此接口会校验是否重复提交
     * @param email        收件人邮箱号
     * @param validateType 验证类型[1-免密登录 2-找回密码 3-用户注册]
     * @param request      http请求对象
     * @return 请求结果
     */
    @Resubmit(message = "发送邮件")
    @GetMapping("/send")
    public Result<Object> sendMail(@RequestParam("email") String email, @RequestParam("validateType") Integer validateType,
                           HttpServletRequest request) {
        log.info("开始发送电子邮件业务。email={} validateType={}", email, validateType);

        if (StringUtils.isBlank(email) || !RegexUtils.isEmail(email)) {
            log.info("发送电子邮件接口请求参数错误");
            return Result.paramFail();
        }
        if (ValidateCodeTypeEnum.checkEmailCodeType(validateType)) {
            log.info("发送短信接口，验证类型参数错误");
            return Result.paramFail();
        }

        mailService.sendMail(email, validateType, request);

        log.info("发送电子邮件业务结束");
        return Result.success();
    }
}
